package asionreachel.cn.smarthomedemo.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import asionreachel.cn.smarthomedemo.R;
import asionreachel.cn.smarthomedemo.base.BaseActivity;
import asionreachel.cn.smarthomedemo.base.Common;
import asionreachel.cn.smarthomedemo.widget.ValidatePhoneView;
import familyclient.android.familymanager.JFamilyManagerInterface;
import familyclient.android.familymanager.JFamilyManagerListener;
import familyclient.android.verificationmanager.JVerificationManagerInterface;
import familyclient.android.verificationmanager.JVerificationManagerListener;
import familyclient.java.verificationmanager.jni.JniVerificationManager;
import nccore.java.data.JBaseTime;

public class RegisterActivity extends BaseActivity {
    private static final String TAG = "RegisterActivity";
    private static final String FUNCTION = "Register";

    private ValidatePhoneView m_vpvVerifyCode;
    private EditText idaccount;


    private JFamilyManagerListener m_listenerFamilyManager = new JFamilyManagerListener()
    {
        @Override
        public void CreateFamilyCb( String a_strErr )
        {
            if( !m_bRun )
                return;

            if( null != a_strErr && a_strErr.length() > 0 )
            {
                // 创建账号失败
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
            else
            {
                // 创建账号成功
                onRegisterSuccess();
            }
        }
    };

    private JVerificationManagerListener m_listenerVerificationManager = new JVerificationManagerListener()
    {
        @Override
        public void DispatchVerificationCodeCb( String a_strErr )
        {
            if( !m_bRun )
                return;

            if( null != a_strErr && a_strErr.length() > 0 )
            {
                //发送验证码失败
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
            else
            {
                // 发送验证码成功
                Common.showToast(R.string.toast_send_verifycode_success);
                m_vpvVerifyCode.onStart();
            }
        }
    };

    public void onBack() {
        mActivity.finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;

        setContentView( R.layout.activity_register );
        idaccount=findViewById(R.id.register_ed_idaccount);
        JFamilyManagerInterface.GetInstance().AddListener( m_listenerFamilyManager );
        JVerificationManagerInterface.GetInstance().AddListener( m_listenerVerificationManager );


        ImageView ivBack = (ImageView)findViewById(R.id.iv_back);
        Button btRegister = (Button)findViewById(R.id.register_btn_yes);
        m_vpvVerifyCode = (ValidatePhoneView)findViewById(R.id.register_vpv_get_code);

        m_vpvVerifyCode.onCreate(savedInstanceState);

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBack();
            }
        });
        btRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRegister();
            }
        });
        m_vpvVerifyCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //在这里发送验证码
                onRequestVerifyCode();
            }
        });

    }

    //完成注册,是直接进入功能界面还是返回登录界面手动登录?
    private void onRegister()
    {
        //验证账号密码是否符合规则
        EditText etVerifyCode = (EditText)findViewById(R.id.register_ed_code);
        String strVerifyCode = etVerifyCode.getText().toString();
//        if (strVerifyCode.isEmpty())
//        {
//            Log.e(TAG , "验证码不能为空");
//            Common.showToast(R.string.toast_null_verifycode);
//            return;
//        }
        EditText etAccount = (EditText)findViewById(R.id.register_ed_account);
        String strAccount = etAccount.getText().toString();
        String strPhone = "";
        String strEmail = "";
//        if (Common.checkEmail(strAccount))
//            strEmail = strAccount;
//        else if (Common.checkPhoneNumber(strAccount))
//            strPhone = strAccount;
//        else
//        {
//            Log.e(TAG , "账号必须为邮箱或者手机号");
//            Common.showToast(R.string.toast_correct_account);
//            return;
//        }
        EditText etPassword1 = (EditText)findViewById(R.id.register_ed_password1);
        EditText etPassword2 = (EditText)findViewById(R.id.register_ed_password2);
        String strPassword1 = etPassword1.getText().toString();
        String strPassword2 = etPassword2.getText().toString();
        if(idaccount.getText().toString().isEmpty()){
            Common.showToast("账号不能为空");
            return;
        }
        if (strPassword1.isEmpty() || strPassword2.isEmpty())
        {
            Log.e(TAG , "密码不能为空");
            Common.showToast(R.string.toast_null_password);
            return;
        }
        else if (!strPassword1.equals(strPassword2))
        {
            Log.e(TAG , "两个密码不相等");
            Common.showToast(R.string.toast_different_password);
            return;
        }

        JFamilyManagerInterface.GetInstance().CreateFamily(idaccount.getText().toString(), strPhone, strEmail, strPassword1, strAccount, strVerifyCode,1,new JBaseTime(),"");
    }

    //注册成功回调
    public void onRegisterSuccess()
    {
        Common.showToast(R.string.toast_account_create_success);
        //保存账号密码
        EditText etAccount = (EditText)findViewById(R.id.register_ed_account);
        String strAccount = idaccount.getText().toString();
        Common.saveAccountId(strAccount);
        EditText etPassword1 = (EditText)findViewById(R.id.register_ed_password1);
        String strPassword1 = etPassword1.getText().toString();
        Common.savePassword(strPassword1);

        //暂时先返回登录界面,如果是返回登录界面的话,需要进行账号密码自动填充
        finish();
    }

    //请求发送验证码
    private void onRequestVerifyCode()
    {
        EditText etVehicle = (EditText)findViewById(R.id.register_ed_account);
        String strVehicle = etVehicle.getText().toString();
        if(Common.checkEmail( strVehicle ) || Common.checkPhoneNumber( strVehicle ) )
        {
            //开始请求验证码
            JVerificationManagerInterface.GetInstance().DispatchVerificationCode( Common.s_strOperateID, Common.s_strOperatePwd, strVehicle, JniVerificationManager.s_iPurposeRegAcc );
        }
        else
        {
            Common.showToast(R.string.toast_correct_account);
        }
    }

    @Override
    protected void onDestroy()
    {
        JVerificationManagerInterface.GetInstance().DelListener( m_listenerVerificationManager );
        JFamilyManagerInterface.GetInstance().DelListener( m_listenerFamilyManager );
        m_vpvVerifyCode.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onBackKey() {
        onBack();
    }
}
